package com.yixia.pea.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yixia.common.exception.ServiceException;
import com.yixia.common.utils.StringUtils;
import com.yixia.pea.domain.LoansInfo;
import com.yixia.pea.domain.credit.LoansBank;
import com.yixia.pea.mapper.LoansInfoMapper;
import com.yixia.pea.service.LoansBankService;
import com.yixia.pea.mapper.LoansBankMapper;
import com.yixia.pea.service.LoansInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

/**
* @author 35038
* @description 针对表【loans_bank】的数据库操作Service实现
* @createDate 2023-09-07 10:47:20
*/
@Service
public class LoansBankServiceImpl extends ServiceImpl<LoansBankMapper, LoansBank>
    implements LoansBankService {


    @Autowired
    private LoansInfoMapper loansInfoMapper;
    @Autowired
    private LoansBankMapper loansBankMapper;

    /**
     * 农行是否已经存在
     * @param loansBank 农行
     * @return 返回结果
     */
    public boolean checkBankNumUnique(LoansBank loansBank){
        // 根据num来查询
        LambdaQueryWrapper<LoansBank> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(LoansBank::getBankNum, loansBank.getBankNum());
        List<LoansBank> list = this.list(queryWrapper);
        if(StringUtils.isNotEmpty(list)){
            return false;
        }
        return false;
    }

    /**
     * 查看银行名是否存在
     * @param loansBank 农行
     * @return 结果
     */
    @Override
    public boolean checkBankNameUnique(LoansBank loansBank) {
        LambdaQueryWrapper<LoansBank> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(LoansBank::getBankName, loansBank.getBankName());
        List<LoansBank> list = this.list(queryWrapper);
        if(StringUtils.isNotEmpty(list)){
            return false;
        }
        return false;
    }




    /**
     * 查询所有银行信息
     * @param loansBank 查询条件
     * @return 返回结构
     */
    @Override
    public List<LoansBank> selectPageList(LoansBank loansBank) {
        return loansBankMapper.selectPageList(loansBank);
    }


    /**
     * 根据银行Id进行删除
     * @param bankIds 要删除的ids
     * @return 结果
     */
    @Override
    @Transactional
    public boolean removeByBankIds(Long[] bankIds){
        // 判断这些id是否已经被使用
        for (Long bankId : bankIds) {
            LambdaQueryWrapper<LoansInfo> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(LoansInfo::getBankId, bankId);

            List<LoansInfo> list = loansInfoMapper.selectList(queryWrapper);
            if(StringUtils.isNotEmpty(list)){
                throw new ServiceException("ID编号为 ‘" + bankId + "‘ 的银行在已经被使用，无法进行删除！");
            }
        }
        // 进行逻辑删除操作
        LambdaUpdateWrapper<LoansBank> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.in(LoansBank::getBankId, Arrays.asList(bankIds));
        updateWrapper.set(LoansBank::getDelFlag, "2");
        return this.update(updateWrapper);
    }

    /**
     * 根据银行Id进行查询详情
     * @param bankId 银行ID
     * @return 返回结果
     */
    @Override
    public boolean checkBankDataUnique(Long bankId){
        LambdaQueryWrapper<LoansBank> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(LoansBank::getBankId, bankId);
        queryWrapper.eq(LoansBank::getDelFlag, "0");
        List<LoansBank> list = this.list(queryWrapper);
        if(StringUtils.isNotEmpty(list)){
            return true;
        }
        return false;
    }

    /**
     * 获取详情
     * @param bankId 银行ID
     * @return 返回结果
     */
    @Override
    public LoansBank getByBankId(Long bankId){
        LambdaQueryWrapper<LoansBank> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(LoansBank::getBankId, bankId).eq(LoansBank::getDelFlag, "0");
        return this.getOne(queryWrapper);
    }



}




